import request from '@/utils/request';
import { requestWrapper, randomDate, randomInt, randomPick, paginate } from '@/utils/mock';

// 获取在线用户列表
export function getOnlineList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/monitor/online/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      // 浏览器
      const browsers = ['Chrome', 'Firefox', 'Safari', 'Edge', 'IE'];
      // 操作系统
      const osList = ['Windows 10', 'Windows 7', 'MacOS', 'Linux', 'Android', 'iOS'];
      // 部门
      const departments = ['总公司', '研发部门', '市场部门', '财务部门', '人事部门'];

      const onlineList = Array.from({ length: 20 }, (_, index) => ({
        tokenId: `token_${randomInt(10000, 99999)}`,
        userId: randomInt(1, 100),
        userName: randomPick(['admin', 'user1', 'user2', 'manager', 'operator']),
        deptName: randomPick(departments),
        ipaddr: `192.168.1.${randomInt(1, 255)}`,
        loginLocation: '内网IP',
        browser: randomPick(browsers),
        os: randomPick(osList),
        loginTime: randomDate(-1) // 在线用户一般是最近一天内登录的
      }));

      // 处理搜索条件
      let filteredList = [...onlineList];

      if (params) {
        if (params.ipaddr) {
          filteredList = filteredList.filter(item => 
            item.ipaddr.includes(params.ipaddr)
          );
        }
        
        if (params.userName) {
          filteredList = filteredList.filter(item => 
            item.userName.includes(params.userName)
          );
        }
      }

      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = pageNum * pageSize;
      const pageData = filteredList.slice(startIndex, endIndex);

      return paginate(pageData, pageNum, pageSize, filteredList.length);
    }
  );
}

// 强制退出用户
export function forceLogout(tokenId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/monitor/online/${tokenId}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
} 